# chfont.tcl - font changer dialog
#
# CONTENTS:
# 	chfont
#		Displays a "Change font" -dialog, waits
#		for user to input a font and changes the
#		application font with it, and sets it also
#		as the default font. Takes no arguments.

proc chfont {} {
	global FONT
	global CONFIG
	global n_conns
	
	if [winfo exists .dlg] {
		return 1
	}
	
	toplevel .dlg
	wm title .dlg {Change font}
	
	ttk::frame	.dlg.fr
	listbox		.dlg.fr.fonts		-yscroll {.dlg.fr.scroll set}
	ttk::scrollbar	.dlg.fr.scroll		-orient vertical -command {.dlg.fr.fonts yview}
	ttk::label	.dlg.fr.fontsize_label	-text "Size:"
	ttk::entry	.dlg.fr.fontsize_entry	
	text		.dlg.fr.preview		-height 8 -width 25
	ttk::button	.dlg.fr.set		-text "Set" -command "set done 1"
	
	# Set default fontsize
	.dlg.fr.fontsize_entry insert end [lindex $FONT 1]
	
	grid .dlg.fr -in .dlg -column 1 -row 1 -sticky nsew
	
	grid .dlg.fr.fonts		-row 0 -column 0 -rowspan 4 -sticky nsew
	grid .dlg.fr.scroll		-row 0 -column 1 -rowspan 4 -sticky nsew
	grid .dlg.fr.fontsize_label	-row 0 -column 2
	grid .dlg.fr.fontsize_entry	-row 0 -column 3
	grid .dlg.fr.preview		-row 1 -column 2 -columnspan 2 -sticky nsew
	grid .dlg.fr.set		-row 3 -column 2 -columnspan 2 -sticky nsew
	
	foreach family [lsort -dictionary [font families]] {
		.dlg.fr.fonts insert end $family
	}
	
	vwait done
	
	set FONT "{[.dlg.fr.fonts get [.dlg.fr.fonts curselection]]} [.dlg.fr.fontsize_entry get]"
	set CONFIG(defaultfont) $FONT
	
	# Change the font of ALL [text] widgets found on the client
	foreach notebook [$n_conns tabs] {
		foreach window [$notebook tabs] {
			$window.mainbox.output configure -font "{[.dlg.fr.fonts get [.dlg.fr.fonts curselection]]} [.dlg.fr.fontsize_entry get]"
			$window.users configure -font "{[.dlg.fr.fonts get [.dlg.fr.fonts curselection]]} [.dlg.fr.fontsize_entry get]"
			
			# Reconfigure the tags so the bits where bold and italic were used change font
			$window.mainbox.output tag delete bold
			$window.mainbox.output tag delete italic
			$window.mainbox.output tag config bold -font [linsert [$window.mainbox.output cget -font] end bold]
			$window.mainbox.output tag config italic -font [linsert [$window.mainbox.output cget -font] end italic]
		}
	}
	
	destroy .dlg.fr.fonts
	destroy .dlg.fr.scroll
	destroy .dlg.fr.fontsize_label
	destroy .dlg.fr.fontsize_entry
	destroy .dlg.fr.preview
	destroy .dlg.fr.set
	destroy .dlg.fr
	destroy .dlg
	
	saveconfig
}
